package com.csun.cmny.provider.controller.life;

import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.DmcCsunSleepBind;
import com.csun.cmny.provider.model.domain.DmcSleepCareBind;
import com.csun.cmny.provider.model.vo.DfcSleepThresholdVo;
import com.csun.cmny.provider.model.dto.life.DfcSleepThresholdDto;
import com.csun.cmny.provider.model.domain.DmcLifeTransducerDefaultConfig;
import com.csun.cmny.provider.service.DmcCsunSleepBindService;
import com.csun.cmny.provider.service.DmcDfcSleepThresholdService;
import com.csun.cmny.provider.service.DmcLifeTransducerDefaultConfigService;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcSleepCareBindService;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Auther: ZNR
 * @Date: 2020-12-16
 * @Description: 德福长床垫阈值controller
 */

@RestController
@RequestMapping(value = "/dfc", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DmcDfcSleepThresholdController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private DmcDfcSleepThresholdService dmcDfcSleepThresholdService;
    @Resource
    private DmcLifeTransducerDefaultConfigService lifeTransducerDefaultConfigService;
    @Resource
    private DmcCsunSleepBindService sleepBindService;
    @Resource
    private DmcSleepCareBindService sleepCareBindService;


    /**
     * 查询床垫配置、阈值
     * @param deviceId
     * @return com.csun.cmny.provider.controller.life.DmcDfcSleepThresholdController
     */
    @PostMapping(value = "/getDfcSleepThreshold/{deviceId}")
    public Wrapper getDfcSleepThreshold(@PathVariable Long deviceId) {

        DfcSleepThresholdVo vo = dmcDfcSleepThresholdService.getDfcSleepThreshold(deviceId);

        //如果这个床垫没有设定阈值,就下发机构阈值
        if(PublicUtil.isEmpty(vo)){
            DmcLifeTransducerDefaultConfig dmcLifeTransducerDefaultConfig = lifeTransducerDefaultConfigService.selectByKey(getLoginAuthDto().getGroupId());
            vo.setMaxBreathThreshold(dmcLifeTransducerDefaultConfig.getBreathMaxTimes());
            vo.setMinBreathThreshold(dmcLifeTransducerDefaultConfig.getBreathMinTimes());
            vo.setMaxHeartbeatThreshold(dmcLifeTransducerDefaultConfig.getHeartbeatMaximum());
            vo.setMinHeartbeatThreshold(dmcLifeTransducerDefaultConfig.getHeartbeatMinimum());
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), vo);
    }

    /**
     * 批量床垫阈值设置
     * @param dto
     * @return com.csun.cmny.provider.controller.life.DmcDfcSleepThresholdController
     */
    @PostMapping(value = "/setDfcSleepThreshold")
    public Wrapper getDfcSleepThreshold(@RequestBody DfcSleepThresholdDto dto) {
        for (Long deviceId : dto.getDeviceIdList()) {
            //校验设备是否绑定了位置
            DmcCsunSleepBind dmcCsunSleepBind = sleepBindService.selectByKey(deviceId);
            DmcSleepCareBind dmcSleepCareBind = sleepCareBindService.selectByKey(deviceId);
            if (PublicUtil.isEmpty(dmcCsunSleepBind)&&PublicUtil.isEmpty(dmcSleepCareBind)){
                return WrapMapper.wrap(Wrapper.ERROR_CODE,
                        cmnyMessageSource.getMessage("device.notbind.error", null));
            }
        }
        int ret = dmcDfcSleepThresholdService.setDfcSleepThreshold(dto,getLoginAuthDto());
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), ret);
    }
}
